package cn.wolfcode.controller;


import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.query.CustomerQueryObject;
import cn.wolfcode.query.JsonResult;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 潜在客户controller
 */
@Api(value = "潜在客户",description = "潜在客户资源控制器")
@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    //处理查询所有部门请求
    @RequiresPermissions(value = {"customer:potentialList","潜在客户列表"},logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String list(Model model,@ModelAttribute("qo") CustomerQueryObject qo){
        //根据字典明细表查询出所有的职业
        List<SystemDictionaryItem> jobs =  systemDictionaryItemService.selectByParentId(1L);
        List<SystemDictionaryItem> sources =  systemDictionaryItemService.selectByParentId(2L);
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectByParentId(26L);
        model.addAttribute("ccts",ccts);
        model.addAttribute("jobs",jobs);
        model.addAttribute("sources",sources);

        //获取用户主体
        Subject subject = SecurityUtils.getSubject();
        //获取登录用户
        Employee employee = (Employee) subject.getPrincipal();
        //如果用户不具有admin角色或Market_Manager角色时，此销售人员,只能看自己的客户,不能看到别人的
        //因为我们根据sellerId进行模糊查询，也就是当前的用户的Id,所以我们把当前用户的Id设置给sellerId即可
        if(!(subject.hasRole("admin") || subject.hasRole("Market_Manager"))){
            qo.setSellerId(employee.getId());
        }

        List<String> roleList = new ArrayList<>();
        roleList.add("市场专员");
        roleList.add("市场经理");
        //查询所有的销售人员
        List<Employee> employeeList =  employeeService.selectSeller(roleList);
        model.addAttribute("employeeList",employeeList);

        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "customer/list";
    }
    // 处理删除请求
    @GetMapping("/delete")
    public String delete(Long id){
        if (id != null) {
            customerService.delete(id);
        }
        return "redirect:/customer/potentialList.do"; // 让浏览器重新发一次请求
    }
    // 去新增或去修改页面
    @RequestMapping("/input")
    public String input(Long id, Model model){
        if (id != null) { // 表示去修改
            Customer customer = customerService.get(id);
            model.addAttribute("customer", customer);
        }
        return "customer/input"; // 让浏览器重新发一次请求
    }
    // 新增或修改
    @RequiresPermissions(value = {"customer:saveOrUpdate","潜在客户保存/更新"},logical = Logical.OR)
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer){
        JsonResult jr = new JsonResult();
        try {
            if(customer.getId() == null){ // 新增
                //获取当前登录对象
                Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
                customer.setSeller(employee);
                customer.setInputUser(employee);
                customer.setInputTime(new Date());
                customerService.save(customer);
            }else {
                customerService.update(customer); // 修改
            }
            jr.setFlag(true);
            jr.setMsg("保存成功");
        } catch (Exception e) {
            jr.setFlag(false);
            jr.setMsg("保存失败");
        }
        return jr;
    }

}

